package com.dex4d.ui.dapp.model;

import android.arch.persistence.room.Entity;
import android.arch.persistence.room.Ignore;
import android.arch.persistence.room.PrimaryKey;
import android.os.Parcel;
import android.os.Parcelable;

import com.dex4d.ui.qr.model.URLParser;
import com.dex4d.ui.qr.utils.AddressUtils;

import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

/**
 * 作者    LiQingyi
 * 时间    2018/12/3 14:16
 * 文件    dex4d-android
 * 描述    浏览历史
 */

@Entity
public class History implements Parcelable {

    @PrimaryKey(autoGenerate = true)
    public long id;

    public String title;//网站标题 ok

    public String url;//浏览地址 ok

    public String md5;//当此值不变的时候调整时间戳 ok

    public String icon;//网站小图标

    public String host;// ok

    public long timestamp;//浏览时间 ok

    /**
     * 0：默认
     * 1：标记为喜欢
     */
    public int bookmark;//是否标记为书签 ok

    public String folder;//所属文件夹

    public String desc; //自定义描述信息

    //其他信息
    public String other1;
    public String other2;
    public String other3;
    public String other4;

    @Ignore
    public boolean isSelected;

    /**
     * 更新时间戳
     */
    public void updateTimestamp() {
        this.timestamp = Calendar.getInstance(Locale.getDefault()).getTimeInMillis();
    }

    public void like() {
        if (this.bookmark == 1) {
            this.bookmark = 0;
        } else {
            this.bookmark = 1;
        }
    }

    public History(String url, String title) {
        this.url = url;
        this.timestamp = Calendar.getInstance(Locale.getDefault()).getTimeInMillis();
        this.md5 = AddressUtils.md5(url);
        this.title = title;
        this.bookmark = 0;

        try {
            URLParser parser = new URLParser(url);
            StringBuilder builder = new StringBuilder();

            builder.append(parser.getProtocol()).append("://");
            builder.append(parser.getHost());
            if (parser.getPort() != null) {
                builder.append(":").append(parser.getPort());
            }
            builder.append("/favicon.ico");

            this.icon = builder.toString();
            this.host = parser.getHost();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    public String getTimestamp(){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar=Calendar.getInstance();
        calendar.setTimeInMillis(timestamp);
        return formatter.format(calendar.getTime());
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(this.id);
        dest.writeString(this.title);
        dest.writeString(this.url);
        dest.writeString(this.md5);
        dest.writeString(this.icon);
        dest.writeString(this.host);
        dest.writeLong(this.timestamp);
        dest.writeInt(this.bookmark);
        dest.writeString(this.folder);
        dest.writeString(this.desc);
        dest.writeString(this.other1);
        dest.writeString(this.other2);
        dest.writeString(this.other3);
        dest.writeString(this.other4);
    }

    protected History(Parcel in) {
        this.id = in.readLong();
        this.title = in.readString();
        this.url = in.readString();
        this.md5 = in.readString();
        this.icon = in.readString();
        this.host = in.readString();
        this.timestamp = in.readLong();
        this.bookmark = in.readInt();
        this.folder = in.readString();
        this.desc = in.readString();
        this.other1 = in.readString();
        this.other2 = in.readString();
        this.other3 = in.readString();
        this.other4 = in.readString();
    }

    public static final Parcelable.Creator<History> CREATOR = new Parcelable.Creator<History>() {
        @Override
        public History createFromParcel(Parcel source) {
            return new History(source);
        }

        @Override
        public History[] newArray(int size) {
            return new History[size];
        }
    };
}
